///oss图片转换工具类，可转换webp，固定宽高webp等(参考链接：https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.help-menu-31815.d_4_11_1_1_0.557774e9FNRCH0&scm=20140722.H_44688._.OR_help-T_cn~zh-V_1)
///其他平台或者自己的服务转换可修改字符串拼接规则
///测试图片：https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit
///可使用ScreenUtils类中的方法获取对应像素尺寸大小
class ImageResizeUtil{
  static const String ossImgLabel = "?x-oss-process=image";
  static const String formatWebp = "/format,webp";

  /// 如果传宽高时，图片按剪切缩放
  static String turnWebp(String imgUrl){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }

    return "$imgUrl?x-oss-process=image$formatWebp";
  }

  /// m_fill 剪切缩放
  /// m_lfit 填充缩放
  /// fillInSide 是否缩放到内部最短边，默认缩放到最长边并剪切
  static String turnWebpWithWH(String imgUrl,int width, int height, {fillInSide}){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }

    if(fillInSide is bool && fillInSide){
      //缩放到最短边，不剪切
      return "$imgUrl?x-oss-process=image/resize,m_lfit,h_$height,w_$width$formatWebp";
    } else {
      //缩放到最长边，剪切超过尺寸的部分，默认是要剪切
      return "$imgUrl?x-oss-process=image/resize,m_fill,h_$height,w_$width$formatWebp";
    }
  }

  static String turnWebpWithWidth(String imgUrl, int width){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,w_$width,m_lfit$formatWebp";
  }

  static String turnWebpWidthHeight(String imgUrl, int height){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,h_$height,m_lfit$formatWebp";
  }

  static String turnWebpRound(String imgUrl, int width, int height,int radius){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,m_fill,h_$height,w_$width/rounded-corners,r_$radius$formatWebp";
  }

  //缩放图片，不转为webp
  static String turnSize(String imgUrl, int width, int height, {fillInSide}){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }

    if(fillInSide is bool && fillInSide){
      //缩放到最短边，不剪切
      return "$imgUrl?x-oss-process=image/resize,m_lfit,h_$height,w_$width";
    } else {
      //缩放到最长边，剪切超过尺寸的部分，默认是要剪切
      return "$imgUrl?x-oss-process=image/resize,m_fill,h_$height,w_$width";
    }
  }

  static String turnSizeWithWidth(String imgUrl, int width){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,w_$width,m_lfit";
  }

  static String turnSizeWithHeight(String imgUrl, int height){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,h_$height,m_lfit";
  }

  ///bgcolor，传入跟控件背景一样颜色即可，默认白色
  static String turnRound(String imgUrl, int width, int height,int radius){
    if(isNullOrTurned(imgUrl)){
      return imgUrl;
    }
    return "$imgUrl?x-oss-process=image/resize,m_fill,h_$height,w_$width/rounded-corners,r_$radius";
  }

  ///是否为空已经转换过
  static bool isNullOrTurned(String imgUrl){
    if(imgUrl.isEmpty){
      return true;
    }
    if(imgUrl.contains("?")) {
      //已添加过后缀时直接返回
      return true;
    }
    return false;
  }
}

// main(){
// print(ImageResizeUtil.turnWebp("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg"));
// print(ImageResizeUtil.turnWebpWithWH("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100));
// print(ImageResizeUtil.turnWebpWithWH("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100, fillInSide: true));
// print(ImageResizeUtil.turnWebpWithWidth("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 50));
// print(ImageResizeUtil.turnWebpWidthHeight("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100));
// print(ImageResizeUtil.turnWebpRound("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100, 100));
// print(ImageResizeUtil.turnSize("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100));
// print(ImageResizeUtil.turnSizeWithWidth("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100));
// print(ImageResizeUtil.turnSizeWithHeight("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100));
// print(ImageResizeUtil.turnRound("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100, 100));
// print(ImageResizeUtil.turnRound("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 100, 100, 100));
// print(ImageResizeUtil.turnWebpWithWH("https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg", 10, 10));
// }